#!/bin/bash

function killmanialive {

        # read process id from file
        read -r PID < .pid

        # check if process is running
        if [ -n "`ps -p ${PID} | grep ${PID}`" ]
        then
                echo "Process found, trying to kill it now ..."

                # try to kill process
                SUCCESS=`kill $PID`
                if [ "$SUCCESS" == "" ]
                then
                        echo "Process has been killed!"
                else
                        echo "Process could not be killed!"
                fi
        else
                echo "Process can not be found, probably already stopped."
        fi

        # remove the pid file
        echo "Removing process id file."
        rm .pid
}

PHP=""

# read run configuration
exec<run.ini
while read line
do
        # do not read comments
        if [ `echo | awk '{ print substr("'"$line"'",1,1) }'` != ";" ]
        then

                # seperate key and value
                key=`echo "$line" | awk -F"=" '{print $1}'`
                value=`echo "$line" | awk -F"=" '{print $2}'`

                # store php path
                if [ $key = "phpPath" ]
                then
                        PHP=`echo $value`
                fi
        fi
done

# set default if no override
if [ "$PHP" = "" ]
then
        PHP=`which php`
fi

# set default if no override - edited on windows.
if [ "$PHP" = `echo -e "\r"` ]
then
        PHP=`which php`
fi

STOP="false"
ARGS=""
DAEMON="true"

# read command line arguments
for i in "$@"
do
        if [ "$i" == "--nodaemon" ]
        then
                DAEMON="false"
        else
                if [ "$i" == "--stop" ]
                then
                STOP="true"
                else
                        if [ "$i" == "--start" ]
                        then
                                STOP="false"
                        else
                        ARGS="$ARGS $i"
                        fi
                fi
        fi
done

# start either daemon or run within console
if [ $DAEMON == "false" ]
then
        echo "Launching ManiaLive with the following arguments: $ARGS"
        $PHP ./bootstrapper.php $ARGS
else
        if [ $STOP == "false" ]
        then
				if [ -f $PWD/.pid ]
				then
					PID=`cat $PWD/.pid`
					PS_PID=`ps -p $PID --noheader | awk '{print $1}'`
					if [ ${PS_PID:-0} -eq ${PID:-0} ]
					then
							echo "ManiaLive is currently running, use --stop to kill it."
							exit
					fi
				fi

                echo "Launching ManiaLive Daemon with the following arguments: $ARGS"
                $PHP $PWD/bootstrapper.php $ARGS </dev/null > $PWD/logs/runtime.log 2>&1 &

                echo "Writing process id : $! to file."
                echo $! > .pid
        else
                echo "Stopping Manialive Daemon ..."
                if [ -f $PWD/.pid ]
                then
                        killmanialive
                else
                        echo "You did not start ManiaLive!"
                fi
        fi
fi